home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / newmat.exe / NEWMAT.HXX < prev    next >
Text File  |  1991-07-30  |  24KB  |  674 lines

  1. //$$ newmat.hxx         definition file for new version of matrix package
  2.  
  3. // Copyright (C) 1991: R B Davies and DSIR
  4.  
  5. #ifndef MATRIX_LIB
  6. #define MATRIX_LIB 0
  7.  
  8. #ifdef NO_LONG_NAMES
  9. #define UpperTriangularMatrix UTMatrix
  10. #define LowerTriangularMatrix LTMatrix
  11. #define SymmetricMatrix SMatrix
  12. #define DiagonalMatrix DMatrix
  13. #endif
  14.  
  15. #include "boolean.hxx"
  16.  
  17.  
  18. /**************************** general utilities ****************************/
  19.  
  20. void MatrixError(char*);                        // error handler
  21. void MatrixErrorNoSpace(void*);                 // no space handler
  22.  
  23. class LogAndSign
  24. // Return from LogDeterminant function
  25. //    - value of the log plus the sign (+, - or 0)
  26. {
  27.    real log_value;
  28.    int sign;
  29. public:
  30.    LogAndSign() { log_value=0.0; sign=1; }
  31.    void operator*=(real);
  32.    void ChangeSign() { sign = -sign; }
  33.    real LogValue() { return log_value; }
  34.    int Sign() { return sign; }
  35.    real Value();
  36. };
  37.  
  38. // the following class is for counting the number of times a piece of code
  39. // is executed. It is used for locating any code not executed by test
  40. // routines. Use turbo GREP locate all places this code is called and
  41. // check which ones are not accessed.
  42. // Somewhat implementation dependent as it relies on "cout" still being
  43. // present when ExeCounter objects are destructed.
  44.  
  45. class ExeCounter
  46. {
  47.    int line;                                    // code line number
  48.    int fileid;                                  // file identifier
  49.    long nexe;                                   // number of executions
  50.    static int nreports;                         // number of reports
  51. public:
  52.    ExeCounter(int,int);
  53.    void operator++() { nexe++; }
  54.    ~ExeCounter();                               // prints out reports
  55. };
  56.  
  57.  
  58. /**************************** class MatrixType *****************************/
  59.  
  60. // Is used for finding the type of a matrix resulting from the binary operations
  61. // +, -, * and identifying what conversions are permissible.
  62. // This class must be updated when new matrix types are added.
  63.  
  64. class GeneralMatrix;                            // defined later
  65.  
  66. class MatrixType
  67. {
  68. public:
  69.    enum Type { UnSp,UT,LT,Rect,Sym,Diag,RowV,ColV,EqEl,Crout };
  70.    static nTypes() { return 8; }               // number of different types
  71.                            // exclude Crout, UnSp
  72. private:
  73.    Type type;
  74. public:
  75.    MatrixType operator+(const MatrixType&) const;
  76.    MatrixType operator-(const MatrixType&) const;
  77.    MatrixType operator*(const MatrixType&) const;
  78.    BOOL operator>=(const MatrixType&) const;
  79.    BOOL operator==(const MatrixType& t) const; // { return (type == t.type); }
  80.    BOOL operator!=(const MatrixType& t) const; // { return !(*this == t); }
  81.    BOOL operator!() const { return type == UnSp; }
  82.    MatrixType operator-() const;               // type of negative
  83.    MatrixType i() const;                       // type of inverse
  84.    MatrixType t() const;                       // type of transpose
  85.    MatrixType sub() const;                     // type of submatrix
  86.    MatrixType ssub() const;                    // type of sym submatrix
  87.    MatrixType (Type tx) : type(tx) {}          // (& doesn't work with AT&T)
  88.    MatrixType () {}
  89.    GeneralMatrix* New() const;                 // new matrix of given type
  90.    GeneralMatrix* New(int,int) const;          // new matrix of given type
  91.    operator int() const { return (int)type; }
  92.    operator char*() const;                     // for printing type
  93. };
  94.  
  95. void TestTypeAdd();                            // test +
  96. void TestTypeMult();                           // test *
  97. void TestTypeOrder();                          // test >=
  98.  
  99. /*************************** Matrix routines ***************************/
  100.  
  101.  
  102. class MatrixRowCol;                             // defined later
  103. class MatrixRow;
  104. class MatrixCol;
  105.  
  106. class GeneralMatrix;                            // defined later
  107. class AddedMatrix;
  108. class MultipliedMatrix;
  109. class SubtractedMatrix;
  110. class SolvedMatrix;
  111. class ShiftedMatrix;
  112. class ScaledMatrix;
  113. class TransposedMatrix;
  114. class NegatedMatrix;
  115. class InvertedMatrix;
  116. class RowedMatrix;
  117. class ColedMatrix;
  118. class DiagedMatrix;
  119. class MatedMatrix;
  120. class GetSubMatrix;
  121. class ConstMatrix;
  122. class Matrix;
  123. class RowVector;
  124. class ColumnVector;
  125. class SymmetricMatrix;
  126. class UpperTriangularMatrix;
  127. class LowerTriangularMatrix;
  128. class DiagonalMatrix;
  129. class CroutMatrix;
  130.  
  131. static MatrixType MatrixTypeUnSp(MatrixType::UnSp);
  132.                                                 // AT&T needs this
  133.  
  134. class BaseMatrix                                // base of all matrix classes
  135. {
  136. protected:
  137. //   BaseMatrix() {}
  138.    virtual GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp) = 0;
  139.                                                 // evaluate temporary
  140.    virtual int search(const GeneralMatrix*) const = 0;
  141.                         // count number of times matrix
  142.                         // is referred to
  143.    virtual MatrixType Type() const = 0;         // type of a matrix
  144.    virtual int NrowsV()  const = 0;
  145.    virtual int NcolsV()  const = 0;
  146. public:
  147.    void MatrixParameters(int& nr, int& nc, MatrixType& mt)
  148.       { nr = NrowsV(); nc = NcolsV(); mt = Type(); }
  149.    AddedMatrix operator+(BaseMatrix&);          // results of operations
  150.    MultipliedMatrix operator*(BaseMatrix&);
  151.    SubtractedMatrix operator-(BaseMatrix&);
  152.    ShiftedMatrix operator+(real);
  153.    ScaledMatrix operator*(real);
  154.    ScaledMatrix operator/(real);
  155.    ShiftedMatrix operator-(real);
  156.    TransposedMatrix t();
  157.    NegatedMatrix operator-();                   // change sign of elements
  158.    InvertedMatrix i();
  159.    RowedMatrix CopyToRow();
  160.    ColedMatrix CopyToColumn();
  161.    DiagedMatrix CopyToDiagonal();
  162.    MatedMatrix CopyToMatrix(int,int);
  163.    GetSubMatrix SubMatrix(int,int,int,int);
  164.    GetSubMatrix SymSubMatrix(int,int);
  165.    GetSubMatrix Row(int);
  166.    GetSubMatrix Rows(int,int);
  167.    GetSubMatrix Col(int);
  168.    GetSubMatrix Cols(int,int);
  169.    virtual LogAndSign LogDeterminant();
  170.    virtual real SumSquare();
  171.    virtual real Trace();
  172. //   virtual ~BaseMatrix() {}
  173.  
  174.    friend GeneralMatrix;
  175.    friend Matrix;
  176.    friend RowVector;
  177.    friend ColumnVector;
  178.    friend SymmetricMatrix;
  179.    friend UpperTriangularMatrix;
  180.    friend LowerTriangularMatrix;
  181.    friend DiagonalMatrix;
  182.    friend CroutMatrix;
  183.    friend AddedMatrix;
  184.    friend MultipliedMatrix;
  185.    friend SubtractedMatrix;
  186.    friend SolvedMatrix;
  187.    friend ShiftedMatrix;
  188.    friend ScaledMatrix;
  189.    friend TransposedMatrix;
  190.    friend NegatedMatrix;
  191.    friend InvertedMatrix;
  192.    friend RowedMatrix;
  193.    friend ColedMatrix;
  194.    friend DiagedMatrix;
  195.    friend MatedMatrix;
  196.    friend GetSubMatrix;
  197.    friend ConstMatrix;
  198. };
  199.  
  200.  
  201. /******************************* working classes **************************/
  202.  
  203. class GeneralMatrix : public BaseMatrix         // declarable matrix types
  204. {
  205. protected:
  206.    int tag;                                     // shows whether can reuse
  207.    int nrows, ncols;                            // dimensions
  208.    int storage;                                 // total store required
  209.    real* store;                                 // point to store (0=not set)
  210.    GeneralMatrix();                             // initialise with no store
  211.    GeneralMatrix(int);                          // constructor getting store
  212.    GeneralMatrix* Evaluate(MatrixType);
  213.    void Add(GeneralMatrix*, real);              // sum of GM and real
  214.    void Add(real);                              // add real to this
  215.    void Multiply(GeneralMatrix*, real);         // product of GM and real
  216.    void Multiply(real);                         // multiply this by real
  217.    void Negate(GeneralMatrix*);                 // change sign
  218.    void Negate();                               // change sign
  219.    void operator=(real);                        // set matrix to constant
  220.    real* GetStore();                            // get store or copy
  221.    GeneralMatrix* BorrowStore(GeneralMatrix*, MatrixType);
  222.                                                 // temporarily access store
  223.    void GetMatrix(GeneralMatrix*);              // used by = and initialise
  224. #ifndef __ZT